package com.sanjiahantang.youjiaclient.module.evaluate.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.Editable;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.FileBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.models.IFileModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.FileModel;
import com.sanjiahantang.youjiaclient.module.evaluate.adapters.MeEvaluateAdapter;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.EvaGoodsBean;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.EvaServiceBean;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.GoodsAppraiseInitResponse;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.RoleBean;
import com.sanjiahantang.youjiaclient.module.evaluate.models.IEvaluateModel;
import com.sanjiahantang.youjiaclient.module.evaluate.models.impl.EvaluateModel;
import com.sanjiahantang.youjiaclient.module.evaluate.views.MeEvaluateView;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.CALLBACK_TYPE_DELETE_PICTURE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.CALLBACK_TYPE_GOODS_DETAIL;
import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.INTENT_SELECT_PIC;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_FILE_BASE64_PIC;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PHONE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;
import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;


/**
 * Created by zy on 2017/9/6.
 */

public class MeEvaluatePresenter extends BaseMvpPresenter<MeEvaluateView> {

    private IEvaluateModel evaluateModel;
    private IFileModel fileModel;
    private MeEvaluateAdapter evaluateAdapter;
    public MeEvaluatePresenter(){
        evaluateModel = new EvaluateModel();
        fileModel = new FileModel();
    }






    public void onCheckedChanged(Context context, int type) {
        onCheckedChanged(context,type,true);

    }

    public void onCheckedChanged(Context context, int type, boolean showProgress) {
        if(showProgress){
            mView.showProgress();
        }

        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
        param.put(KEY_PARAM,map);
        if(type == MeEvaluateView.TYPE_GOODS_EVA){

            evaluateModel.loadGoodsEvaData(context,param,new GoodsEvaCallBack());
        }else {
            evaluateModel.loadServiceEvaData(context,param,new ServiceEvaCallBack());
        }
    }

    public void findGoodsAppraiseForCustomer(Context context, String orderCode, final BaseView.OnLoadListener onLoadListener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("orderCode",orderCode);
        mView.showProgress();
        evaluateModel.findGoodsAppraiseForCustomer(context,param, new CallBack<GoodsAppraiseInitResponse>() {
            @Override
            public void onSuccess(GoodsAppraiseInitResponse response, Context context, Object tag) {
                hideProgress();
                Object[] param = new Object[2];
                param[0] = CALLBACK_TYPE_GOODS_DETAIL;
                param[1] = response.getData();
                onLoadListener.onLoad(param);
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }

    public void doUploadGoodsEvaluate(Context context, Uri uri, int busiId, final BaseView.OnLoadListener listener) {

        try {
            Bitmap bitmap = Utils.getBitmapFormUri((Activity) context,uri);
            doUploadGoodsEvaluate(context,bitmap,busiId,listener);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doUploadGoodsEvaluate(Context context, Bitmap bitmap, int busiId, final BaseView.OnLoadListener listener) {

        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("fileId","0");
        param.put("busiId",String.valueOf(busiId));
        param.put("busiTypeId","goodsAppraise");
        param.put("fileName",System.currentTimeMillis() + ".jpg");
        param.put(KEY_FILE_BASE64_PIC, Utils.bitmapToBase64(bitmap));

        mView.showProgress();
        fileModel.uploadMallFile(context,param, new CallBack<FileBean>() {
            @Override
            public void onSuccess(FileBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    public void doUploadServiceEvaluate(Context context, Bitmap bitmap, int busiId, final BaseView.OnLoadListener listener) {

        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("fileId","0");
        param.put("busiId",String.valueOf(busiId));
        param.put("busiTypeId","serviceAppraise");
        param.put("fileName",System.currentTimeMillis() + ".jpg");
        param.put(KEY_FILE_BASE64_PIC, Utils.bitmapToBase64(bitmap));

        mView.showProgress();
        fileModel.uploadMallFile(context,param, new CallBack<FileBean>() {
            @Override
            public void onSuccess(FileBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    public void deleteFile(Context context, final int id, final int position, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("fileId",String.valueOf(id));
        mView.showProgress();
        fileModel.deleteMallFile(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    Object[] obj = new Object[3];
                    obj[0] = CALLBACK_TYPE_DELETE_PICTURE;
                    obj[1] = id;
                    obj[2] = position;
                    listener.onLoad(obj);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    public void saveAppraiseForCustomer(final Context context, final List<GoodsAppraiseInitResponse.Data> listData, final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("mallGoodsAppraiseList",listData);
        mView.showProgress();
        evaluateModel.saveAppraiseForCustomer(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getMessage());
                    Utils.showToast(context,response.getMessage());
                }else if (response != null && !TextUtils.isEmpty(response.getMessage())){
                    Utils.showToast(context,response.getMessage());
                }else{
                    Utils.showToast(context,"保存失败");
                    ((Activity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                if (context != null){
                    Utils.showToast(context,"保存失败");
                    ((Activity)context).finish();
                }
            }
        });
    }

    public void searchDesignRolePhone(final Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("orderCode",orderCode);
        evaluateModel.searchDesignRolePhone(context,param, new CallBack<RoleBean>() {
            @Override
            public void onSuccess(RoleBean response, Context context, Object tag) {
                if (mView == null ) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }else{
                    Utils.showToast(context,"获取人员信息失败");
                    ((Activity)context).finish();
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null ) return;
                mView.hideProgress();
                Utils.showToast(context,"获取人员信息失败");
                ((Activity)context).finish();
            }
        });
    }

    public void deleteImage(Context context, final int id, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("fileId",String.valueOf(id));
        mView.showProgress();
        fileModel.deleteMallFile(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(id);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }

    public void doUploadPic(Context context, Uri uri, int maxId, final BaseView.OnLoadListener listener) {
        Bitmap bitmap = null;
        try {
            bitmap = Utils.getBitmapFormUri((Activity) context,uri);
            Map<String,String> param = new HashMap<>();
            param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
            param.put("fileId","0");
            param.put("busiId",String.valueOf(maxId));
            param.put("busiTypeId","serviceAppraise");
            param.put("fileName",System.currentTimeMillis() + ".jpg");
            param.put(KEY_FILE_BASE64_PIC, Utils.bitmapToBase64(bitmap));

            mView.showProgress();
            fileModel.uploadMallFile(context,param, new CallBack<FileBean>() {
                @Override
                public void onSuccess(FileBean response, Context context, Object tag) {
                    if (mView == null) return;
                    mView.hideProgress();
                    if (response != null){
                        if (response.getCode() == RESPONSE_CODE_SUC){
                            listener.onLoad(response.getData());
                        }else{
                            Utils.showToast(context,response.getMessage());
                        }
                    }else{
                        Utils.showToast(context,"上传失败");
                    }

                }

                @Override
                public void onFailure(String message) {
                    if (mView == null) return;
                    mView.hideProgress();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveServiceAppraisePhone(final Context context, int msId,String txtContentText, int rating,RoleBean.Data role,String orderCode,BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);

        Map<String,Object> content = new HashMap<>();

        content.put("msId",msId);
        content.put("phone",SPUtils.get(context,SPConfig.PHONE,"").toString());
        content.put("rolePhone",role.getRolePhone());
        content.put("roleName",role.getRoleName());
        content.put("roleTypeId",role.getRoleTypeId());
        content.put("roleTypeName",role.getRoleTypeName());
        content.put("userRequireId",role.getUserRequireId());
        content.put("place",txtContentText);
        content.put("userNumber",rating);
        content.put("orderCode",orderCode);
        content.put("money",role.getMoney());

        ArrayList< Map<String,Object>> list = new ArrayList<>();
        list.add(content);

        param.put("serviceAppraiseList",list);
        evaluateModel.saveServiceAppraisePhone(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    Utils.showToast(context,"保存成功");
                    ((Activity)context).finish();
                }else{
                    Utils.showToast(context,"保存失败");
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context,"保存失败");
            }
        });
    }

    public void searchProjectRolePhone(final Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("orderCode",orderCode);
        evaluateModel.searchProjectRolePhone(context,param, new CallBack<RoleBean>() {
            @Override
            public void onSuccess(RoleBean response, Context context, Object tag) {
                if (mView == null ) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }else{
                    Utils.showToast(context,"获取人员信息失败");
                    ((Activity)context).finish();
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null ) return;
                mView.hideProgress();
                Utils.showToast(context,"获取人员信息失败");
                ((Activity)context).finish();
            }
        });
    }

    public void saveServiceAppraisePhone(final Context context, int maxId1, String content1, int rating, RoleBean.Data role1,
                                         int maxId2, String content2, int rating1, RoleBean.Data role2, String orderCode, BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);

        Map<String,Object> content = new HashMap<>();

        content.put("msId",maxId1);
        content.put("phone",SPUtils.get(context,SPConfig.PHONE,"").toString());
        content.put("rolePhone",role1.getRolePhone());
        content.put("roleName",role1.getRoleName());
        content.put("roleTypeId",role1.getRoleTypeId());
        content.put("roleTypeName",role1.getRoleTypeName());
        content.put("userRequireId",role1.getUserRequireId());
        content.put("place",content1);
        content.put("userNumber",rating);
        content.put("orderCode",orderCode);
        content.put("money",role1.getMoney());

        ArrayList< Map<String,Object>> list = new ArrayList<>();
        list.add(content);


        Map<String,Object> content0 = new HashMap<>();

        content0.put("msId",maxId2);
        content0.put("phone",SPUtils.get(context,SPConfig.PHONE,"").toString());
        content0.put("rolePhone",role2.getRolePhone());
        content0.put("roleName",role2.getRoleName());
        content0.put("roleTypeId",role2.getRoleTypeId());
        content0.put("roleTypeName",role2.getRoleTypeName());
        content0.put("userRequireId",role2.getUserRequireId());
        content0.put("place",content2);
        content0.put("userNumber",rating1);
        content0.put("orderCode",orderCode);
        content0.put("money",role2.getMoney());

        list.add(content0);

        param.put("serviceAppraiseList",list);
        evaluateModel.saveServiceAppraisePhone(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    Utils.showToast(context,"保存成功");
                    ((Activity)context).finish();
                }else if (response != null && !TextUtils.isEmpty(response.getMessage())){
                    Utils.showToast(context,response.getMessage());
                }else{
                    Utils.showToast(context,"保存失败");
                    ((Activity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context,"保存失败");
            }
        });

    }



    private class ServiceEvaCallBack implements CallBack<EvaServiceBean>{

        @Override
        public void onSuccess(EvaServiceBean response, Context context, Object tag) {
            mView.hideProgress();
            evaluateAdapter = new MeEvaluateAdapter(context,MeEvaluateAdapter.TYPE_SERVICE,response);
            mView.loadData(evaluateAdapter);
        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }

    private class GoodsEvaCallBack implements CallBack<EvaGoodsBean> {

        @Override
        public void onSuccess(EvaGoodsBean response, Context context, Object tag) {
            mView.hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC){
                evaluateAdapter = new MeEvaluateAdapter(context,MeEvaluateAdapter.TYPE_GOODS,response);
                mView.loadData(evaluateAdapter);
            }

        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }

    public void cardSelect(Activity activity) {
        Intent it = new Intent(Intent.ACTION_PICK);
        it.setType("image/*");
        activity.startActivityForResult(it, INTENT_SELECT_PIC);
    }
}
